<template>
  <div class="login-bg">
    <div class="login-glass">
      <div class="login-title">综合办公系统登录</div>
      <el-form :model="form" class="login-form" @submit.prevent="adminLogin">
        <el-form-item>
          <el-input v-model="form.username" placeholder="用户名" clearable />
        </el-form-item>
        <el-form-item>
          <el-input v-model="form.password" type="password" placeholder="密码" clearable @keyup.enter="adminLogin" />
        </el-form-item>
        <div class="login-btn-group">
          <el-button class="login-btn main-btn" type="primary" size="large" @click="adminLogin">用户登录</el-button>
          <el-button class="login-btn main-btn" size="large" @click="guestLogin">访客模式</el-button>
        </div>
        <div class="login-link-row">
          <el-button type="link" class="forgot-link" @click="showForgotDialog = true">忘记密码？</el-button>
        </div>
        <div class="login-error" v-if="errorMsg">{{ errorMsg }}</div>
        <div class="login-tip">如需注册，请联系超级管理员</div>
      </el-form>
      <el-dialog v-model="showForgotDialog" title="找回密码" width="350px">
        <el-form :model="forgotForm">
          <el-form-item label="用户名">
            <el-input v-model="forgotForm.username" placeholder="请输入用户名" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="showForgotDialog = false">取消</el-button>
          <el-button type="primary" @click="forgotPassword">提交</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import AV from '../leancloud.js'
import { ElMessage } from 'element-plus'
const emit = defineEmits(['login'])
const form = ref({ username: '', password: '' })
const errorMsg = ref('')
const showForgotDialog = ref(false)
const forgotForm = ref({ username: '' })

async function adminLogin() {
  errorMsg.value = ''
  try {
    await AV.User.logIn(form.value.username, form.value.password)
    emit('login', { isGuest: false })
  } catch (e) {
    errorMsg.value = '账号或密码错误，或云端校验失败！'
  }
}
function guestLogin() {
  emit('login', { isGuest: true })
}

async function forgotPassword() {
  if (!forgotForm.value.username) {
    ElMessage.error('请输入用户名')
    return
  }
  try {
    const query = new AV.Query('UserProfile')
    query.equalTo('username', forgotForm.value.username)
    const profile = await query.first()
    if (!profile) {
      ElMessage.error('未找到该用户')
      return
    }
    profile.set('needChangePwd', true)
    await profile.save()
    ElMessage.success('已标记为需重置密码，请联系管理员')
    showForgotDialog.value = false
  } catch (e) {
    ElMessage.error('操作失败：' + (e.message || e.rawMessage))
  }
}
</script>

<style scoped>
.login-bg {
  position: fixed;
  left: 0; top: 0; right: 0; bottom: 0;
  width: 100vw; height: 100vh;
  background: url('https://bing.img.run/rand.php') center center/cover no-repeat;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.login-glass {
  background: rgba(255,255,255,0.18);
  box-shadow: 0 8px 32px 0 rgba(31,38,135,0.18);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-radius: 22px;
  border: 1.5px solid rgba(255,255,255,0.22);
  padding: 48px 36px 36px 36px;
  min-width: 320px;
  max-width: 380px;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.login-title {
  font-size: 2.2rem;
  font-weight: bold;
  color: #222;
  margin-bottom: 32px;
  letter-spacing: 2px;
  text-shadow: 0 2px 8px rgba(0,0,0,0.08);
}
.login-form {
  width: 100%;
}
.login-form .el-form-item {
  width: 90% !important;
  margin: 0 auto 18px auto;
  padding: 0;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.login-form .el-input {
  width: 100% !important;
  margin: 0 0 12px 0;
  border-radius: 10px !important;
  font-size: 17px;
  display: block;
}
.login-form .el-input__wrapper {
  background: rgba(255,255,255,0.92) !important;
  border-radius: 10px !important;
  box-shadow: 0 2px 8px 0 rgba(31,38,135,0.08);
  border: 1px solid #e0e3ea !important;
  transition: border-color 0.2s;
  min-height: 44px;
}
.login-form .el-input__wrapper:hover,
.login-form .el-input__wrapper.is-focus {
  border-color: #409eff !important;
  box-shadow: 0 2px 12px 0 rgba(64,158,255,0.10);
}
.login-btn-group {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin: 18px 0 8px 0;
  align-items: center;
  width: 100%;
}
.login-btn {
  width: 220px !important;
  min-height: 48px !important;
  border-radius: 12px !important;
  font-size: 18px !important;
  font-weight: 500;
  letter-spacing: 2px;
  box-shadow: 0 2px 8px 0 rgba(31,38,135,0.08);
  display: block;
  margin: 0 auto !important;
}
.main-btn {
  margin-bottom: 0 !important;
}
.login-link-row {
  width: 100%;
  text-align: right;
  margin-bottom: 8px;
}
.forgot-link {
  font-size: 15px !important;
  color: #409eff !important;
  padding: 0 6px;
  min-width: unset !important;
}
.login-error {
  color: #d9001b;
  margin-top: 8px;
  text-align: center;
  font-size: 15px;
}
.login-tip {
  color: #666;
  font-size: 15px;
  text-align: center;
  margin-top: 14px;
  letter-spacing: 1px;
}
</style>
